package com.xilihui.xlh.business.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.TakeCodeEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/30.
 */

public class TakeGoodCodeActivity extends BaseCompatActivity{
    
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    String id = "";
    
    BaseAdapter<TakeCodeEntity.DepotOrderGoodsBean> beanBaseAdapter;
    ArrayList<TakeCodeEntity.DepotOrderGoodsBean> datas = new ArrayList<>();
    
    @Override
    public int getLayout() {
        return R.layout.activity_take_good_code;
    }

    @Override
    public String initTitle() {
        return "领货码";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        id = getIntent().getStringExtra("id");
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        View view = LayoutInflater.from(this).inflate(R.layout.recycler_take_good_code_head,null);
        SimpleDraweeView sdv = (SimpleDraweeView)view.findViewById(R.id.sdv_code_img);
        String access_token = (String) SPUtil.get(this,SPUtil.TOKEN,"");
        sdv.setImageURI(UrlConst.baseUrl()+"/api/depot_order/use_goods_decode?access_token="+access_token+"&id="+id);
        recyclerView.addHeaderView(view);
        recyclerView.setPullRefreshEnabled(false);
        
        beanBaseAdapter = new BaseAdapter<TakeCodeEntity.DepotOrderGoodsBean>(this,datas) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_take_good;
            }

            @Override
            protected void getView(BaseViewHolder holder, TakeCodeEntity.DepotOrderGoodsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_img);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOriginal_img());
                
                holder.setText(R.id.tv_name,entity.getGoods_name());
                holder.setText(R.id.tv_model,entity.getSpec_key_name());
                holder.setText(R.id.tv_num,"x"+entity.getGoods_num());
                
            }
            
        };
        
        
        recyclerView.setAdapter(beanBaseAdapter);
        beanBaseAdapter.setAnimationsLocked(true);
        
        
    }

    @Override
    public void initData() {
        getData();
    }
    
    public void getData(){
        MyAppointmentRequest.takeCode(this,id)
                .compose(DoTransform.<TakeCodeEntity>applyScheduler(this,true))
                .compose(this.<TakeCodeEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<TakeCodeEntity>(this) {
                    @Override
                    protected void onSuccess(TakeCodeEntity entity) {
                        datas.clear();
                        datas.addAll(entity.getDepot_order_goods());
                        beanBaseAdapter.setList(datas);
                    }
                                                                
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortPositive(msg);
                    }
                });
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is(StrConst.UMENG_PUSH)){
            finish();
        }
    }
}
